<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_LocationMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Location();
	}

	public function getLocationById($location_id, $user_id) {
		$result = $this->_db_table->find($location_id);
		$db_user = new Api_Model_DbTable_User();
		$row = $result->current();

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		}
		//
		//        $this->db_city = new Api_Model_DbTable_City();
		//        $s_city = $this->db_city->select()->from('cities', array('*'))->where('city_id = ?', $row->city_id);
		//        $re_city = $this->db_city->fetchAll($s_city);
		//        $row_city = $re_city->current();
		//
		//
		//photo
		$this->db_photo = new Api_Model_DbTable_Photo();
		$s_photo = $this->db_photo->select()->from('photos', array('*'))->where('location_id = ?', $row->location_id);
		$re_photo = $this->db_photo->fetchAll($s_photo);
		$count_photo = count($re_photo);
		if(count($re_photo) == 0) {
			$photos= '';
		} else {
			foreach ($re_photo as $row){
				$select_user = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->user_id);
				$result_user = $this->_db_table->fetchAll($select_user);
				$row_user = $result_user->current();
				//get user info
				$user = array('user_id' => $row_user->user_id,
                              'username' => $row_user->username,
                              'fullname' => $row_user->fullname,
                              'profile_picture' => $row_user->profile_picture
				);

				$photos[] = array('photo' => array('photo_id' => $row->photo_id,
                                                    'photo_link' => $row->photo_link,
                                                    'created_time' => $row->created_time,
                                                    'user' => $user));
			}
		}
		 

		//like
		$this->db_tour_like = new Api_Model_DbTable_Like();
		$s_like = $this->db_tour_like->select()->from('likes', array('*'))->where('location_id = ?', $row->location_id);
		$re_like = $this->db_tour_like->fetchAll($s_like);
		$count_like = count($re_like);

		//check like
		$s_clike = $this->db_tour_like->select()->from('likes', array('*'))->where('location_id = ?', $row->location_id)
		->where('user_id = ?', $user_id);
		$re_clike = $this->db_tour_like->fetchAll($s_clike);
		if(count($re_clike) == 0) {
			$liked = 0;
		} else
		$liked = 1;

		//check in
		$this->db_tour_location = new Api_Model_DbTable_TourLocation();
		$s_tl = $this->db_tour_location->select()->from('tour_location', array('*'))->where('location_id = ?', $row->location_id);
		$re_tl = $this->db_tour_location->fetchAll($s_tl);
		$count_tl = count($re_tl);

		//comments
		$this->db_comment = new Api_Model_DbTable_Comment();
		$s_comment = $this->db_comment->select()->from('comments', array('*'))->where('location_id = ?', $row->location_id);
		$re_comment = $this->db_comment->fetchAll($s_comment);
		$count_comment = count($re_comment);
		if(count($re_comment) == 0) {
			$comments = '';
		} else {
			foreach ($re_comment as $row){
				$select_user = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->user_id);
				$result_user = $this->_db_table->fetchAll($select_user);
				$row_user = $result_user->current();
				//get user info
				$user = array('user_id' => $row_user->user_id,
                              'username' => $row_user->username,
                              'fullname' => $row_user->fullname,
                              'profile_picture' => $row_user->profile_picture
				);

				$comments[] = array('comment' => array('comment_id' => $row->comment_id,
                                                    'content' => $row->content,
                                                    'created_time' => $row->created_time,
                                                    'user' => $user));
			}
		}

		$json = array('like_count' => $count_like,
                        'checkin_count' => $count_tl,
                        'review_count' => $count_comment,
                        'photo_count' => $count_photo,
                        'comments' => $comments,
                        'photos' => $photos,
                        'liked' => $liked);

		return $json;
	}

	public function getAllLocation() {
		$select = $this->_db_table->select()->from('locations', array('*'));

		$result = $this->_db_table->fetchAll($select);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				//$rowArray = $row->toArray();
				//get city name
				$this->db_city = new Api_Model_DbTable_City();
				$s_city = $this->db_city->select()->from('cities', array('*'))->where('city_id = ?', $row->city_id);
				$re_city = $this->db_city->fetchAll($s_city);
				$row_city = $re_city->current();

				$location = array('location_id' => $row->location_id,
                                  'name' => $row->name,
                                  'description' => $row->description,
                                  'latitude' => $row->latitude,
                                  'longitude' => $row->longitude,
                    "city" => array('city_id' => $row_city->city_id, 'name' => $row_city->name));

				$json[] = array('location' => $location);

			}

			return $json;
		}
	}


	public function createLocation($data){

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$currentDateTime = date('Y-m-d H-i-s');
		$affectedRow = $this->_db_table->insert($data);

		if ($affectedRow){
			$location_id = $this->_db_table->getAdapter()->lastInsertId();
			$this->_db_table->update(array('created_time' => $currentDateTime),
			array('tour_id = ?' => $location_id));

		} else {
			throw new Exception('Internal Server Error' ,500);
		}
	}

	public function updateLocation($data, $location_id){
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$this->_db_table->update($data, 'location_id = '.$location_id);

	}

	public function searchLocation($locationname) {
		$select = $this->_db_table->select()->from('locations', '*')
		->where('name LIKE ?', '%'.$locationname.'%');
		$result = $this->_db_table->fetchAll($select);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				//get city name
				$this->db_city = new Api_Model_DbTable_City();
				$s_city = $this->db_city->select()->from('cities', array('*'))->where('city_id = ?', $row->city_id);
				$re_city = $this->db_city->fetchAll($s_city);
				$row_city = $re_city->current();


				$location = array('location_id' => $row->location_id,
                                  'name' => $row->name,
                                  'description' => $row->description,
                                  'latitude' => $row->latitude,
                                  'longitude' => $row->longitude,
                    "city" => array('city_id' => $row_city->city_id, 'name' => $row_city->name));

				$json[] = array('location' => $location);

			}
			return $json;
		}
	}


	public function searchNearLocation($latitude, $longitude, $locationname) {
		$select = $this->_db_table->select()->from('locations', array('*' ,'distance' => '( 6371 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) *
                    cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') ) * sin( radians( latitude ) ) ) )'))
		->where('name LIKE ?', '%'.$locationname.'%')
		->having('distance < 100')
		->order('distance')
		->limit(20, 0);
		$result = $this->_db_table->fetchAll($select);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				//get city name
				$this->db_city = new Api_Model_DbTable_City();
				$s_city = $this->db_city->select()->from('cities', array('*'))->where('city_id = ?', $row->city_id);
				$re_city = $this->db_city->fetchAll($s_city);
				$row_city = $re_city->current();


				$location = array('location_id' => $row->location_id,
                                  'name' => $row->name,
                                  'description' => $row->description,
                                  'latitude' => $row->latitude,
                                  'longitude' => $row->longitude,
                    "city" => array('city_id' => $row_city->city_id, 'name' => $row_city->name));

				$json[] = array('location' => $location);

			}
			return $json;
		}
	}

}
